package de.duester.genealogy;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class NewFamilyTreeActivity extends Activity implements OnClickListener {
	private EditText etDBName;
	private Button btnOk;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_family_tree);
		init();
	}

	private void init() {
		etDBName = (EditText) findViewById(R.id.etDBName);

		btnOk = (Button) findViewById(R.id.btnNewFamilyTreeOk);
		btnOk.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		String s = etDBName.getText().toString().trim();
		if (s.equals("")) {
			Toast.makeText(this, R.string.msgFamilyTreeNameEmpty, Toast.LENGTH_SHORT)
					.show();
			etDBName.requestFocus();
		} else if (getDatabasePath(s + ".db").exists()) {
			Toast.makeText(this, getString(R.string.msgFamilyTreeDBExists, s),
					Toast.LENGTH_SHORT).show();
			etDBName.requestFocus();
		} else {
			Intent intent = new Intent();
			intent.putExtra("dbName", s);
			setResult(RESULT_OK, intent);
			finish();
		}
	}

}
